﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;

namespace YJKOpeningDetail
{
	internal class WallAndGridInfo
	{
		public bool GetWallOrGridInfo(Document doc, ViewPlan viewPlan, List<double> lstLevel, ref Dictionary<XYZ, DataWall> dicWallCurve, ref Dictionary<XYZ, List<Curve>> dicCimbineGridCurve)
		{
			this.m_lstLevelValue = lstLevel;
			if (!OpeningViewDataSetForm.BRadSelGrid)
			{
				Dictionary<Curve, double> dicCombineWallInfo = new Dictionary<Curve, double>();
				if (!this.GetSelectViewWallCurve(doc, viewPlan, ref dicCombineWallInfo))
				{
					return false;
				}
				if (!this.GetAllWallInfo(dicCombineWallInfo, ref dicWallCurve))
				{
					return false;
				}
			}
			else
			{
				List<Curve> lstGridCurve = new List<Curve>();
				if (!this.GetAllGridCurve(doc, ref lstGridCurve))
				{
					return false;
				}
				if (!this.GetCombineGridInfo(lstGridCurve, ref dicCimbineGridCurve))
				{
					return false;
				}
			}
			return true;
		}

		private bool GetAllGridCurve(Document doc, ref List<Curve> lstGridCurve)
		{
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(doc);
			List<BaseLinkDocumentResultBean> list = filteredElementCollectorLinkDocument.OfCalss(typeof(Grid)).OfCategory(BuiltInCategory.OST_Grids).ToBeans();
			if (list.Count < 0)
			{
				return false;
			}
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in list)
			{
				Element useElement = baseLinkDocumentResultBean.UseElement;
				Grid grid = useElement as Grid;
				if (grid != null)
				{
					if (baseLinkDocumentResultBean.UseLinkeInstance == null)
					{
						Curve curve = grid.Curve;
						lstGridCurve.Add(curve);
					}
					else
					{
						Transform totalTransform = baseLinkDocumentResultBean.GetTotalTransform();
						if (totalTransform != null)
						{
							Curve curve = grid.Curve;
							curve = curve.CreateTransformed(totalTransform);
							lstGridCurve.Add(curve);
						}
					}
				}
			}
			return true;
		}

		private bool GetCombineGridInfo(List<Curve> lstGridCurve, ref Dictionary<XYZ, List<Curve>> dicCimbineGridCurve)
		{
			try
			{
				if (dicCimbineGridCurve == null)
				{
					return false;
				}
				for (int i = 0; i < lstGridCurve.Count; i++)
				{
					if (!lstGridCurve[i].IsCyclic)
					{
						YJKCurve hycurve = new YJKCurve(lstGridCurve[i]);
						for (int j = i + 1; j < lstGridCurve.Count; j++)
						{
							if (!lstGridCurve[j].IsCyclic)
							{
								List<Curve> list = new List<Curve>();
								XYZ xyz = hycurve.IntersectionPoint(lstGridCurve[j]);
								if (xyz != null)
								{
									list.Add(lstGridCurve[i]);
									list.Add(lstGridCurve[j]);
									dicCimbineGridCurve.Add(xyz, list);
								}
							}
						}
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return dicCimbineGridCurve.Count >= 1;
		}

		private bool GetSelectViewWallCurve(Document doc, ViewPlan viewPlan, ref Dictionary<Curve, double> dicWallCurve)
		{
			BoundingBoxIntersectsFilter input = null;
			PublicFunction.GetViewBox(viewPlan, this.m_lstLevelValue, ref input, true);
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(doc);
			filteredElementCollectorLinkDocument.AddViewLimit(input);
			List<BaseLinkDocumentResultBean> list = filteredElementCollectorLinkDocument.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToBeans();
			if (list != null && list.Count < 1)
			{
				return false;
			}
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in list)
			{
				Element useElement = baseLinkDocumentResultBean.UseElement;
				Wall wall = useElement as Wall;
                if (wall != null && (int)wall.WallType.Kind != 2 && (int)wall.WallType.Kind != 1)
				{
					double wallWidth = PublicFunction.GetWallWidth(wall);
					if (baseLinkDocumentResultBean.UseLinkeInstance == null)
					{
						LocationCurve locationCurve = wall.Location as LocationCurve;
						Curve curve = locationCurve.Curve;
						if (curve is Line)
						{
							dicWallCurve.Add(curve, wallWidth);
						}
					}
					else
					{
						Transform totalTransform = baseLinkDocumentResultBean.GetTotalTransform();
						LocationCurve locationCurve2 = wall.Location as LocationCurve;
						Curve curve2 = locationCurve2.Curve;
						if (totalTransform != null)
						{
							curve2 = curve2.CreateTransformed(totalTransform);
							if (curve2 is Line)
							{
								dicWallCurve.Add(curve2, wallWidth);
							}
						}
					}
				}
			}
			return true;
		}

		private bool GetAllWallInfo(Dictionary<Curve, double> dicCombineWallInfo, ref Dictionary<XYZ, DataWall> dicWallCurve)
		{
			try
			{
				if (dicWallCurve == null)
				{
					return false;
				}
				for (int i = 0; i < dicCombineWallInfo.Count; i++)
				{
					KeyValuePair<Curve, double> keyValuePair = dicCombineWallInfo.ElementAt(i);
					YJKCurve hycurve = new YJKCurve(keyValuePair.Key);
					if (!keyValuePair.Key.IsCyclic)
					{
						for (int j = i + 1; j < dicCombineWallInfo.Count; j++)
						{
							DataWall dataWall = new DataWall();
							KeyValuePair<Curve, double> keyValuePair2 = dicCombineWallInfo.ElementAt(j);
							if (!keyValuePair2.Key.IsCyclic)
							{
								XYZ xyz = hycurve.IntersectionPoint(keyValuePair2.Key);
								if (xyz != null)
								{
									dataWall.curvrFirst = keyValuePair.Key;
									dataWall.curveSecond = keyValuePair2.Key;
									dataWall.dFirstWallWidth = keyValuePair.Value;
									dataWall.dSecondWallWidth = keyValuePair2.Value;
									dicWallCurve.Add(xyz, dataWall);
								}
							}
						}
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return dicWallCurve.Count >= 1;
		}

		private List<double> m_lstLevelValue;
	}
}
